package com.example.meet.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.framework.base.BaseActivity;
import com.example.framework.bmob.bmobManager.BmobManager;
import com.example.framework.helper.FileHelper;
import com.example.framework.manager.DialogManager;
import com.example.framework.utils.LogUtils;
import com.example.framework.view.DialogView;
import com.example.framework.view.LoadingView;
import com.example.meet.R;

import java.io.File;

import cn.bmob.v3.exception.BmobException;
import de.hdodenhof.circleimageview.CircleImageView;

public class FirstUploadActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_camera;
    private TextView tv_album;
    private TextView tv_cancel;

    private DialogView mPhotoSelectedView;

    private LoadingView loadingView;
    private File uploadFile = null;

    /**
     * 跳转
     *
     * @param activity
     * @param requestCode
     */
    public static void startActivity(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, FirstUploadActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    private CircleImageView iv_photo;
    private EditText et_nickname;
    private Button btn_upload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_upload);

        initView();
    }

    private void initView() {
        initPhotoView();

        iv_photo = findViewById(R.id.iv_photo);
        et_nickname = findViewById(R.id.et_nickname);
        btn_upload = findViewById(R.id.btn_upload);

        iv_photo.setOnClickListener(this);
        btn_upload.setOnClickListener(this);

        btn_upload.setEnabled(false);

        et_nickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 如果nickname有输入
                if (s.length() > 0) {
                    btn_upload.setEnabled(uploadFile != null);
                } else {
                    btn_upload.setEnabled(false);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }

    /**
     * 初始化选择框
     */
    private void initPhotoView() {
        loadingView = new LoadingView(this);
        loadingView.setLoadingText("正在上传...");

        mPhotoSelectedView = DialogManager.getDialogInstance()
                .initView(this, R.layout.dialog_select_photo, Gravity.BOTTOM);
        tv_camera = (TextView) mPhotoSelectedView.findViewById(R.id.tv_camera);
        tv_camera.setOnClickListener(this);
        tv_album = (TextView) mPhotoSelectedView.findViewById(R.id.tv_album);
        tv_album.setOnClickListener(this);
        tv_cancel = (TextView) mPhotoSelectedView.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_camera:
                DialogManager.getDialogInstance().hideDialog(mPhotoSelectedView);
                // 跳转到相机
                FileHelper.getInstance().toCamera(this);
                break;

            case R.id.tv_album:
                DialogManager.getDialogInstance().hideDialog(mPhotoSelectedView);
                // 跳转到相册
                FileHelper.getInstance().toAlbum(this);
                break;

            case R.id.tv_cancel:
                DialogManager.getDialogInstance().hideDialog(mPhotoSelectedView);
                break;

            case R.id.iv_photo:
                // 显示选择提示框
                DialogManager.getDialogInstance().showDialog(mPhotoSelectedView);
                break;

            case R.id.btn_upload:
                uploadPhoto();
                break;
        }
    }

    private void uploadPhoto() {
        // 如果不满足条件，是不会走到这里的
        String nickname = et_nickname.getText().toString().trim();
        loadingView.show();
        BmobManager.getInstance().uploadFirstPhoto(nickname, uploadFile, new BmobManager.OnUploadPhotoListener() {
            @Override
            public void onUploadDone() {
                loadingView.hide();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onUploadFail(BmobException e) {
                loadingView.hide();
                Toast.makeText(FirstUploadActivity.this, "error: " + e.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        LogUtils.i("requestCode：" + requestCode);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == FileHelper.CAMERA_REQUEST_CODE) {
                uploadFile = FileHelper.getInstance().getTempFile();
            } else if (requestCode == FileHelper.ALBUM_REQUEST_CODE) {
                Uri uri = data.getData();
                if (uri != null) {
                    // 真实地址
                    String path = FileHelper.getInstance().getReadPathFromURI(this, uri);
                    if (!TextUtils.isEmpty(path)){
                        uploadFile = new File(path);
                    }
                }
            }
            // 设置头像
            if (uploadFile != null) {
                Bitmap mBitmap = BitmapFactory.decodeFile(uploadFile.getPath());
                iv_photo.setImageBitmap(mBitmap);
                String nickname = et_nickname.getText().toString().trim();
                btn_upload.setEnabled(nickname.length() !=  0);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}